package com.jd.ty.web;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jd.ty.pojo.Dictionary;
import com.jd.ty.pojo.Employee;
import com.jd.ty.pojo.Field;
import com.jd.ty.pojo.Function;
import com.jd.ty.pojo.Role;
import com.jd.ty.service.DictionaryService;
import com.jd.ty.service.EmployeeService;
import com.jd.ty.service.ExportService;
import com.jd.ty.service.FunctionService;
import com.jd.ty.service.RoleService;
import com.jd.ty.service.exception.ServiceException;
import com.jd.ty.util.Pager;
import com.jd.ty.web.constant.DictionaryCode;
import com.jd.ty.web.filter.EmployeeFilter;

public class EmployeeAction extends BaseAction {

	private static final long serialVersionUID = -4018571604337249963L;

	private Long id;

	private EmployeeFilter filter;

	private EmployeeService employeeService;
	private ExportService exportService;

	private Employee employee = new Employee();
	private List<Function> functions;
	private FunctionService functionService;
	private DictionaryService dictionaryService;
	private RoleService roleService;

	private List<Dictionary> departments;

	private List<Dictionary> degrees;
	private List<Dictionary> socialStatus;
	private List<Dictionary> sex;
	private List<Dictionary> registeredResidenceType;
	private List<Dictionary> salary;
	private List<Dictionary> socialSecurityStatus;
	private List<Dictionary> socialSecurityType;
	private List<Dictionary> socialSecuritySort;
	private List<Dictionary> socialSecurityCardStatus;
	private List<Dictionary> healthSecurityCardStatus;
	private List<Dictionary> trainingState;
	private List<Dictionary> marriagementState;
	private List<Dictionary> socialSecurityCompany;
	private List<Dictionary> unifiedEntrance;
	private List<Dictionary> quitJobPapers;

	private List<Role> roles;

	private InputStream fileStream;
	private String fileName;
    private List<Field> fields;
    private String fieldIds;
	@Override
	public String execute() throws Exception {
		loadResources();
		return SUCCESS;
	}

	public String employeeList() {
		pager = getPagerObject();
		List<Employee> res = (List<Employee>) employeeService.getList(pager, getFilter().toHqlString(),
				getSortStr());
		pager.setDataRows(res);
		return SUCCESS;

	}

	public String employeeInfo() {
		if (id != null) {
			employee = employeeService.get(id);
		}
		loadResources();
		return SUCCESS;
	}

	public String employeeDetail() {
		employee = employeeService.get(id);
		return SUCCESS;
	}

	private void loadResources() {
		departments = dictionaryService.getList(DictionaryCode.DEPARTMENT);
		degrees = dictionaryService.getList(DictionaryCode.DEGREE);
		socialStatus = dictionaryService.getList(DictionaryCode.SOCIAL_STATUS);
		sex = dictionaryService.getList(DictionaryCode.SEX);
		registeredResidenceType = dictionaryService.getList(DictionaryCode.REGISTERED_RESIDENSE_TYPE);
		salary = dictionaryService.getList(DictionaryCode.SALARY_TPYE);
		socialSecurityStatus = dictionaryService.getList(DictionaryCode.SOCIAL_SECURITY_STATUS);
		socialSecurityType = dictionaryService.getList(DictionaryCode.SOCIAL_SECURITY_TYPE);
		socialSecuritySort = dictionaryService.getList(DictionaryCode.SOCIAL_SECURITY_SORT);
		socialSecurityCardStatus = dictionaryService.getList(DictionaryCode.SOCIAL_SECURITY_CARD_STATUS);
		healthSecurityCardStatus = dictionaryService.getList(DictionaryCode.HEALTH_SECURITY_CARD_STATUS);
		trainingState = dictionaryService.getList(DictionaryCode.TRAINING_STATE);
		marriagementState = dictionaryService.getList(DictionaryCode.MARRIAGEMENT_STATE);
		socialSecurityCompany = dictionaryService.getList(DictionaryCode.SOCIAL_SECURITY_COMPANY);
		unifiedEntrance = dictionaryService.getList(DictionaryCode.UNIFIED_ENTRANCE);
		quitJobPapers = dictionaryService.getList(DictionaryCode.QUIT_JOB_PAPERS);
		roles = roleService.getList(null);
		fields = exportService.getFields(Field.TYPE_EMPLOYEE);
	}

	public String employeeUpdate() throws ServiceException {
		try {
			if (employee.getEmployeeId() == null) {
				employeeService.add(employee);
			} else {
				employeeService.update(employee);
			}
		} catch (ServiceException e) {
			addActionError(e.getErrorMessage());
			return INPUT;
		}
		return SUCCESS;
	}

	public String delete() {
		try {
			employeeService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public String employeesByDepartment() {
		pager = getPagerObject();
		List<Employee> res = (List<Employee>) employeeService.getEmployeeByDepartment(id);
		pager.setDataRows(res);

		return SUCCESS;
	}
	public String export() {
		try {
			
			List<Employee> employees = employeeService.getList(null);
			Map<String, Object> multiSheet = new HashMap<String, Object>();
			for (Employee employee : employees) {
				multiSheet.put(employee.getEmployeeName(), employee);
			}
			String filePath = exportService.exportExcel("employee", multiSheet);
			fileName = new String("员工信息".getBytes(), "ISO8859-1");
			fileStream = new FileInputStream(filePath);
		} catch (ServiceException e) {
			log.error(e);
			e.printStackTrace();
			addActionError(e.getErrorMessage());
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	
	public String exportAll() {
		try {
			List<Employee> employees = employeeService.getList(null);
			fields=exportService.getFieldsByIds(fieldIds);
			Map<String, List> multiSheet = new HashMap<String, List>();
			multiSheet.put("员工信息", employees);
			String filePath = exportService.exportExcel("portrait", multiSheet, fields);
			fileName = new String("员工信息".getBytes(), "ISO8859-1");
			fileStream = new FileInputStream(filePath);
		} catch (ServiceException e) {
			log.error(e);
			e.printStackTrace();
			addActionError(e.getErrorMessage());
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Pager getPager() {
		return pager;
	}

	public List<Function> getFunctions() {
		return functions;
	}

	public void setFunctions(List<Function> functions) {
		this.functions = functions;
	}

	public FunctionService getFunctionService() {
		return functionService;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	public void setFilter(EmployeeFilter filter) {
		this.filter = filter;
	}

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {

		this.page = page;

	}

	public EmployeeFilter getFilter() {
		if (filter == null) {
			filter = new EmployeeFilter();
		}

		return filter;
	}

	public DictionaryService getDictionaryService() {
		return dictionaryService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public List<Dictionary> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Dictionary> departments) {
		this.departments = departments;
	}

	public List<Dictionary> getDegrees() {
		return degrees;
	}

	public void setDegrees(List<Dictionary> degrees) {
		this.degrees = degrees;
	}

	public List<Dictionary> getSocialStatus() {
		return socialStatus;
	}

	public void setSocialStatus(List<Dictionary> socialStatus) {
		this.socialStatus = socialStatus;
	}

	public List<Dictionary> getSex() {
		return sex;
	}

	public void setSex(List<Dictionary> sex) {
		this.sex = sex;
	}

	public List<Dictionary> getRegisteredResidenceType() {
		return registeredResidenceType;
	}

	public void setRegisteredResidenceType(List<Dictionary> registeredResidenceType) {
		this.registeredResidenceType = registeredResidenceType;
	}

	public List<Dictionary> getSalary() {
		return salary;
	}

	public void setSalary(List<Dictionary> salary) {
		this.salary = salary;
	}

	public List<Dictionary> getSocialSecurityStatus() {
		return socialSecurityStatus;
	}

	public void setSocialSecurityStatus(List<Dictionary> socialSecurityStatus) {
		this.socialSecurityStatus = socialSecurityStatus;
	}

	public List<Dictionary> getSocialSecurityType() {
		return socialSecurityType;
	}

	public void setSocialSecurityType(List<Dictionary> socialSecurityType) {
		this.socialSecurityType = socialSecurityType;
	}

	public List<Dictionary> getSocialSecuritySort() {
		return socialSecuritySort;
	}

	public void setSocialSecuritySort(List<Dictionary> socialSecuritySort) {
		this.socialSecuritySort = socialSecuritySort;
	}

	public List<Dictionary> getSocialSecurityCardStatus() {
		return socialSecurityCardStatus;
	}

	public void setSocialSecurityCardStatus(List<Dictionary> socialSecurityCardStatus) {
		this.socialSecurityCardStatus = socialSecurityCardStatus;
	}

	public List<Dictionary> getHealthSecurityCardStatus() {
		return healthSecurityCardStatus;
	}

	public void setHealthSecurityCardStatus(List<Dictionary> healthSecurityCardStatus) {
		this.healthSecurityCardStatus = healthSecurityCardStatus;
	}

	public List<Dictionary> getUnifiedEntrance() {
		return unifiedEntrance;
	}

	public void setUnifiedEntrance(List<Dictionary> unifiedEntrance) {
		this.unifiedEntrance = unifiedEntrance;
	}

	public List<Dictionary> getQuitJobPapers() {
		return quitJobPapers;
	}

	public void setQuitJobPapers(List<Dictionary> quitJobPapers) {
		this.quitJobPapers = quitJobPapers;
	}

	public List<Dictionary> getTrainingState() {
		return trainingState;
	}

	public void setTrainingState(List<Dictionary> trainingState) {
		this.trainingState = trainingState;
	}

	public List<Dictionary> getMarriagementState() {
		return marriagementState;
	}

	public void setMarriagementState(List<Dictionary> marriagementState) {
		this.marriagementState = marriagementState;
	}

	public List<Dictionary> getSocialSecurityCompany() {
		return socialSecurityCompany;
	}

	public void setSocialSecurityCompany(List<Dictionary> socialSecurityCompany) {
		this.socialSecurityCompany = socialSecurityCompany;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public InputStream getFileStream() {
		return fileStream;
	}

	public void setFileStream(InputStream fileStream) {
		this.fileStream = fileStream;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setExportService(ExportService exportService) {
		this.exportService = exportService;
	}

	public List<Field> getFields() {
		return fields;
	}

	public void setFields(List<Field> fields) {
		this.fields = fields;
	}

	public String getFieldIds() {
		return fieldIds;
	}

	public void setFieldIds(String fieldIds) {
		this.fieldIds = fieldIds;
	}



}
